<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    width="80%"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
          <el-form-item label="栏目名称" prop="columnId"
                        :rules="[
                          {required: true, message:'栏目名称不能为空', trigger:'blur'}
                 ]">
            <SelectTree
              ref="columnTree"
              :props="{
                value: 'id',             // ID字段名
                label: 'name',         // 显示名称
                children: 'children'    // 子级字段名
              }"
              v-if="visible"
              :data="columnList"
              :value="inputForm.columnId"
              :clearable="true"
              :accordion="true"
              @getValue="(value) => {inputForm.columnId=value}"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分类名称" prop="contentTypeId"
                        :rules="[

                 ]">
          <el-select v-model="inputForm.contentTypeId" placeholder="请选择分类" size="medium">
            <el-option
              v-for="item in contentTypeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="标题" prop="title"
                :rules="[
                  {required: true, message:'标题不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.title" placeholder="请填写标题"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="标题颜色" prop="color"
                :rules="[
                 ]">
              <el-color-picker v-model="inputForm.color"></el-color-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="视频" prop="video"
                :rules="[
                 ]">
              <el-input v-model="inputForm.video" placeholder="请填写视频"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="关键字" prop="keywords"
                :rules="[
                 ]">
              <el-input v-model="inputForm.keywords" placeholder="请填写关键字"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="描述摘要" prop="description"
                :rules="[
                 ]">
              <el-input v-model="inputForm.description" placeholder="请填写描述摘要"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="首页轮播标记" prop="carouselFlag"
                :rules="[
                 ]">
              <el-switch
                v-model="inputForm.carouselFlag"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0">
              </el-switch>
           </el-form-item>
        </el-col>

        <el-col :span="12">
            <el-form-item label="首页推荐" prop="recomFlag"
                :rules="[
                 ]">
              <el-switch
                v-model="inputForm.recomFlag"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0">
              </el-switch>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="置顶权重" prop="topWeight"
                :rules="[
                 ]">
              <el-input-number size="small" v-model="inputForm.topWeight" :min="1"></el-input-number>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="置顶期限" prop="topDate"
                :rules="[
                 ]">
              <el-date-picker
                v-model="inputForm.topDate"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="设置置顶期限">
              </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="发布时间" prop="releaseDate"
                :rules="[
                  {required: true, message:'发布时间不能为空', trigger:'blur'}
                 ]">
              <el-date-picker
                v-model="inputForm.releaseDate"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="设置发布时间">
              </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="来源网站名" prop="copyfromSite"
                :rules="[
                 ]">
              <el-input v-model="inputForm.copyfromSite" placeholder="请填写来源网站名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="来源URL" prop="copyfromUrl"
                :rules="[
                  {validator: validator.isURL, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.copyfromUrl" placeholder="请填写来源URL"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="封面图片" prop="image">
            <multi-upload v-model="inputForm.image"
                          upload-directory="/cms/contents/faceImage"
                          :limit="5"></multi-upload>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="推荐图片" prop="recomImage"
                        :rules="[
                 ]">
            <multi-upload v-model="inputForm.recomImage"
                          upload-directory="/cms/contents/recomImage"
                          :limit="5"></multi-upload>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="轮播图片" prop="carouselImage"
                        :rules="[
                 ]">
            <multi-upload v-model="inputForm.carouselImage"
                          upload-directory="/cms/contents/carouselImage"
                          :limit="5"></multi-upload>
          </el-form-item>
        </el-col>
        <!--<el-col>
          <el-tag type="info">正文内容</el-tag>
          <vue-editor useCustomImageHandler @image-added="$utils.handleImageAdded" v-model="richText"/>
        </el-col>-->
        <el-col>
          <el-tag type="info">正文内容</el-tag>
          <Tinymce ref="content" v-model="richText"></Tinymce>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
import SelectTree from '@/components/treeSelect/treeSelect.vue'
import Tinymce from '@/components/Tinymce'
import MultiUpload from '@/components/multiUpload/multiUpload.vue'

export default {
  props: {
    columnList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      inputForm: {
        id: '',
        columnId: '',
        contentTypeId: '',
        title: '',
        color: '',
        image: '',
        video: '',
        keywords: '',
        description: '',
        carouselFlag: '',
        carouselImage: '',
        recomFlag: '',
        recomImage: '',
        topWeight: '',
        topDate: '',
        releaseDate: '',
        contents: '',
        hits: '',
        copyfromSite: '',
        copyfromUrl: '',
        remarks: ''
      },
      contentTypeList: []
    }
  },
  computed: {
    richText: {
      get: function () {
        return this.inputForm.contents.replace(this.inputForm.contents ? /&(?!#?\w+;)/g : /&/g, '&amp;')
          .replace(/&lt;/g, '<')
          .replace(/&gt;/g, '>')
          .replace(/&quot;/g, '"')
          .replace(/&#39;/g, '\'')
      },
      set: function (newValue) {
        this.inputForm.contents = newValue
      }
    }
  },
  watch: {
    'inputForm.columnId': {
      handler (newVal, oldVal) {
        if (!!newVal && !!oldVal && newVal !== oldVal) {
          this.inputForm.contentTypeId = null
        }

        if (newVal) {
          let url = '/cms/contenttype/cmsContentType/allList?column.id=' + newVal
          this.$http({
            url: url,
            method: 'get'
          }).then(({data}) => {
            this.contentTypeList = data.data
          })
        }
      },
      immediate: true,
      deep: false
    }
  },
  components: {
    SelectTree,
    Tinymce,
    MultiUpload
  },
  methods: {
    init (method, obj) {
      this.method = method
      this.inputForm.id = obj.id
      if (method === 'add') {
        this.title = `新建发布内容表`
      } else if (method === 'edit') {
        this.title = '修改发布内容表'
      } else if (method === 'view') {
        this.title = '查看发布内容表'
      }
      this.visible = true
      this.loading = false
      this.carouselImages = []
      this.recomImages = []
      this.faceImages = []
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          this.$http({
            url: `/cms/content/cmsContents/queryById?id=${this.inputForm.id}`,
            method: 'get'
          }).then(({data}) => {
            this.inputForm = this.recover(this.inputForm, data.cmsContents)
            this.$nextTick(() => {
              if (this.$refs['content']) {
                this.$refs['content'].setContent(this.richText || '')
              }
            })
            this.loading = false
          })
        } else {
          this.inputForm.columnId = obj.columnId
          this.$nextTick(() => {
            if (this.$refs['content']) {
              this.$refs['content'].setContent('')
            }
          })
        }
      })
    },
      // 表单提交
    doSubmit () {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/cms/content/cmsContents/save`,
            method: 'post',
            data: this.inputForm
          }).then(({data}) => {
            this.loading = false
            if (data && data.success) {
              this.visible = false
              this.$message.success(data.msg)
              this.$emit('refreshDataList')
            }
          })
        }
      })
    }
  }
}
</script>
